#include "widget.h"
#include "ui_widget.h"
#include<QTimer>
#include<QKeyEvent>
#include<QToolButton>
#include <QGraphicsPixmapItem>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(800,800);
    mGameView.setSceneRect(QRect(0,0,800,800));
    mScene.setSceneRect(QRect(0,0,800,800));//游戏进行场景
    mStartScene.setSceneRect(QRect(0,0,800,800));
    mStartScene.addPixmap(QPixmap(":/img/qt图片/4fceff09c5e559de1a662d894f42874.jpg"));//进入界面

    auto startBtn=new QToolButton();
    startBtn->setAutoRaise(true);
    startBtn->setIcon(QIcon(":/img/qt图片/anniu.png"));
    startBtn->setIconSize(QSize(100,80));
    startBtn->move(350,650);
    //开始游戏
    connect(startBtn,&QToolButton::clicked,[this](){

        mGameView.setScene(&mScene);
        mGameView.show();
        this->mMediaBG.setMedia(QUrl(":/img/qt图片/bgm.mp3 "));
        this->mMediaBG.play();

    });
    mStartScene.addWidget(startBtn);

    mBackGround1.setPixmap(QPixmap(":/img/qt图片/1c7d0bd8d7ad8feaabb945caae5b40b.png"));//背景图片
    mBackGround2.setPixmap(QPixmap(":/img/qt图片/1c7d0bd8d7ad8feaabb945caae5b40b.png"));
    mPlane->setPixmap(QPixmap(":/img/qt图片/红车.png"));

    //设置元素位置

    mBackGround2.setPos(0,-mBackGround2.pixmap().height());

    //图片元素添加到场景
    mScene.addItem(&mBackGround1);
    mScene.addItem(&mBackGround2);
    mScene.addItem(mPlane);

    //设置视图场景
    mGameView.setScene(&mScene);
    mGameView.setScene(&mStartScene);
    mGameView.setParent(this);
    mGameView.show();

    //开启背景移动定时器
    mBGMoveTimer=new QTimer(this);
    mBGMoveTimer->start(10);
    connect(mBGMoveTimer,&QTimer::timeout,this,&Widget::BGMove);

    //开启移动检测定时器
    mPlaneMoveTimer=new QTimer(this);
    mPlaneMoveTimer->start(10);
    connect(mPlaneMoveTimer,&QTimer::timeout,this,&Widget::PlaneMove);




    //开启黄车创建
    mEnemyCreateTimer=new QTimer(this);
    mEnemyCreateTimer->start(3000);
    connect(mEnemyCreateTimer,&QTimer::timeout,this,&Widget::CreateEnemy);

    //开启黄车移动定时器
    mEnemyMoveTimer=new QTimer(this);
    mEnemyMoveTimer->start(10);
    connect(mEnemyMoveTimer,&QTimer::timeout,[this](){
        for(auto enemy:mEnemyList)
        {
            enemy->EnemyMove();
        }
        //碰撞检测
        Collision();
    });



    //开启氮气瓶创建
    mZenemyCreateTimer=new QTimer(this);
    mZenemyCreateTimer->start(3000);
    connect(mZenemyCreateTimer,&QTimer::timeout,this,&Widget::CreateZenemy);

    //开启氮气瓶移动定时器
    mZenemyMoveTimer=new QTimer(this);
    mZenemyMoveTimer->start(10);
    connect(mZenemyMoveTimer,&QTimer::timeout,[this](){
        for(auto zenemy:mZenemyList)
        {
            zenemy->ZenemyMove();
        }
        //碰撞检测
        Zollision();
    });

    //开启卡车创建
    mBenemyCreateTimer=new QTimer(this);
    mBenemyCreateTimer->start(3000);
    connect(mBenemyCreateTimer,&QTimer::timeout,this,&Widget::CreateBenemy);

    //开启卡车移动定时器
    mBenemyMoveTimer=new QTimer(this);
    mBenemyMoveTimer->start(10);
    connect(mBenemyMoveTimer,&QTimer::timeout,[this](){
        for(auto benemy:mBenemyList)
        {
            benemy->BenemyMove();
        }
        //碰撞检测
        Bollision();
    });

//标签

    scorelabel->setParent(this);
    scorelabel->move(60,60);
    scorelabel->setFont(QFont("华文行楷",16));
    scorelabel->setText(QString("分数：%1   ").arg(score));
    scorelabel->setStyleSheet("color:red;");


}

Widget::~Widget()
{
    delete ui;
}

void Widget::BGMove()
{
    mBackGround1.moveBy(0,2.5);//移动量
    mBackGround2.moveBy(0,2.5);


    if(mBackGround1.y()>=mBackGround1.pixmap().height())
    {
        mBackGround1.setY(-mBackGround1.pixmap().height());
    }
    else if(mBackGround2.y()>=mBackGround2.pixmap().height())
    {
        mBackGround2.setY(-mBackGround2.pixmap().height());
    }
}

void Widget::PlaneMove()
{
    for(int keyCode :mKeyList)
    {
        switch(keyCode)
        {
        case Qt::Key_W:mPlane->moveBy(0,-1*mMoveSpeed);break;
        case Qt::Key_S:mPlane->moveBy(0,1*mMoveSpeed);break;
        case Qt::Key_A:mPlane->moveBy(-1*mMoveSpeed,0);break;
        case Qt::Key_D:mPlane->moveBy(1*mMoveSpeed,0);break;
        }
    }

    //边界判断
    if(mPlane->x()<0)
    {
        mPlane->setX(0);
    }
    if(mPlane->y()<0)
    {
        mPlane->setY(0);
    }
    if(mPlane->x()>this->width()-mPlane->pixmap().width())
    {
        mPlane->setX(this->width()-mPlane->pixmap().width());
    }
    if(mPlane->y()>this->height()-mPlane->pixmap().height())
    {
        mPlane->setY(this->height()-mPlane->pixmap().height());
    }
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    //添加按键组合
    switch(event->key())
    {
        case Qt::Key_W:
        case Qt::Key_S:
        case Qt::Key_A:
        case Qt::Key_D:
        mKeyList.append(event->key());
        break;
    }
}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    //移除对应按键组合
    if(mKeyList.contains(event->key()))
    {
        mKeyList.removeOne(event->key());
    }

}

void Widget::CreateEnemy()
{
    QPixmap pixmap(":/img/qt图片/黄车.png");
    int randX=qrand()%(800-pixmap.width());
    Enemy*enemy=new Enemy(QPoint(randX,-200),pixmap);


    mScene.addItem(enemy);
    mEnemyList.append(enemy);
}

void Widget::CreateZenemy()
{
    QPixmap pixmap(":/img/qt图片/氮气.png");
    int randY=qrand()%(800-pixmap.width());
    Zenemy*zenemy=new Zenemy(QPoint(randY,-200),pixmap);

    //添加到场景
    mScene.addItem(zenemy);

    //添加到管理器
    mZenemyList.append(zenemy);
}

void Widget::CreateBenemy()
{
    QPixmap pixmap(":/img/qt图片/货车.png");
    int randZ=qrand()%(800-pixmap.width());
    Benemy*benemy=new Benemy(QPoint(randZ,-200),pixmap);


   //添加到场景
    mScene.addItem(benemy);

    //添加到管理器
    mBenemyList.append(benemy);
}

void Widget::Collision()
{
    for(int i=0;i<mEnemyList.size();i++)
    {
        if(mPlane->collidesWithItem(mEnemyList[i]))//碰撞检测
        {
            //移除场景
            mScene.removeItem(mEnemyList[i]);

            //移除管理器
            mEnemyList.removeOne(mEnemyList[i]);


            mOverScene.setSceneRect(QRect(0,0,800,800));
            mOverScene.addPixmap(QPixmap(":/img/qt图片/游戏结束.jpg"));//结束界面
            mGameoverView.setScene(&mOverScene);
            mGameoverView.setParent(this);
            mGameoverView.show();


        }
    }
}
void Widget::Zollision()
{
    for(int i=0;i<mZenemyList.size();i++)
    {
        if(mPlane->collidesWithItem(mZenemyList[i]))//碰撞检测
        {

            score=score+10;
            scorelabel->setText(QString("分数：%1   ").arg(score));
            mMoveSpeed++;

            //移除场景
            mScene.removeItem(mZenemyList[i]);

            //移除管理器
            mZenemyList.removeOne(mZenemyList[i]);

        }
    }

}



void Widget::Bollision()
{
    for(int i=0;i<mBenemyList.size();i++)
    {
        if(mPlane->collidesWithItem(mBenemyList[i]))//碰撞检测
        {
            //移除场景
            mScene.removeItem(mBenemyList[i]);

            //移除管理器
            mBenemyList.removeOne(mBenemyList[i]);
            mOverScene.setSceneRect(QRect(0,0,800,800));
            mOverScene.addPixmap(QPixmap(":/img/qt图片/游戏结束.jpg"));//结束界面
            mGameoverView.setScene(&mOverScene);
            mGameoverView.setParent(this);
            mGameoverView.show();
        }
    }
}

